﻿layui.use(["form", "layer", "jquery", "upload"], function () {
    var form = layui.form;
    var layer = layui.layer;
    var $ = layui.jquery;
    var upload = layui.upload;

    $(document).ready(function () {
        $("span.peerInfo").click(onPeerInfoClick);
        $("span.receipt").click(onReceiptClick);

        var els = $("div.list-item[isBuyer='1']");
        for (var i = 0; i < els.length; i++) {
            var el = els.eq(i);

            var orderId = el.attr("orderId");
            var sellerUserId = el.attr("sellerUserId");
            upload.render({
                elem: el.find("span[spanBtn='1']").get(0),
                url: "/Order/UploadPaymentVoucher",
                data: {
                    OrderId: orderId,
                    SellerUserId: sellerUserId
                },
                size: 1000,
                before: function (obj) {
                    var el2 = $(this.item).parent().parent();
                    var path = el2.attr("paymentVoucherImagePath");
                    var flag = false;
                    if (path != "" && path != undefined) {
                        var paths = path.split(",");
                        if (paths.length <= 2) {
                            flag = true;
                        }
                    } else {
                        flag = true;
                    }
                    if (flag == false) {
                        layer.msg(addStyle("最多支持3张进货凭证！"), { icon: 2, shade: 0.01, time: 3 * 1000, closeBtn: true });
                        return false;
                    }
                },
                done: function (res, index, upload) {
                    //console.log(res);
                    if (res.Success == true) {
                        var el2 = $(this.item).parent().parent();
                        var path = el2.attr("paymentVoucherImagePath");
                        var path2 = "";
                        if (path == "" || path == undefined || path == null) {
                            path2 = res.Data;
                        } else {
                            path2 = path + "," + res.Data;
                        }
                        el2.attr("paymentVoucherImagePath", path2);
                        layer.msg(addStyle("上传成功！"), { icon: 1, shade: 0.01, time: 1 * 1000 });
                    } else {
                        layer.msg(addStyle("上传失败！" + res.Msg), { icon: 2, shade: 0.01, time: 3 * 1000, closeBtn: true });
                    }
                },
                error: function (index, upload) {
                    layer.msg(addStyle("上传失败！" + res.Msg), { icon: 2, shade: 0.01, time: 3 * 1000, closeBtn: true });
                }
            });

            el.find("span[spanBtn='2']").eq(0).click(onViewVoucherClick);
            el.find("span[spanBtn='3']").eq(0).click(onStockGoodClick);
            el.find("span[spanBtn='4']").eq(0).click(onPickupClick);
        }

        var els2 = $("div.list-item[isSeller='1']");
        for (var i = 0; i < els2.length; i++) {
            var el = els2.eq(i);
            el.find("span[spanBtn='5']").eq(0).click(onViewVoucher2Click);
        }
    });

    function onPeerInfoClick() {
        var self = $(this);
        var sellerUserId = self.parent().parent().attr("sellerUserId");
        var orderId = self.parent().parent().attr("orderId");

        var loadingIndex = layer.msg(addStyle("正在处理 ......"), { icon: 16, shade: 0.01, time: false });
        $.ajax("/Order/PeerInfo", {
            type: "POST",
            data: {
                OrderId: orderId,
                SellerUserId: sellerUserId
            },
            complete: function (jqXHR, textStatus) {
                layer.close(loadingIndex);
            },
            success: function (data, textStatus, jqXHR) {
                //console.log(data);
                if (data.Success == true) {
                    //console.log(data.Data);
                    showUserAccountInfoDialog(data.Data);
                } else {
                    layer.msg(addStyle("查看对方信息失败: " + data.Msg), { icon: 2, time: 3 * 1000, closeBtn: true });
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                layer.msg(addStyle("系统错误，请联系管理员！"), { icon: 2, time: 3 * 1000, closeBtn: true });
            }
        });
    }

    function addStyle(str) {
        return '<span style="font-size: 16px;">' + str + '</span>';
    }

    function showUserAccountInfoDialog(data) {
        //console.log(data);

        var bank = "&nbsp;&nbsp;";
        if (data != null && data.Bank != null) {
            bank = data.Bank;
        }
        var bankCardNo = "&nbsp;&nbsp;";
        if (data != null && data.BankCardNo != null) {
            bankCardNo = data.BankCardNo;
        }
        var bankAccountName = "&nbsp;&nbsp;";
        if (data != null && data.BankAccountName != null) {
            bankAccountName = data.BankAccountName;
        }

        var html = '';
        html += '<div class="layui-tab" lay-filter="test-hash" style="padding: 10px; padding-bottom: 0px;">';
        html += '   <ul class="layui-tab-title">';
        html += '       <li class="layui-this" lay-id="tab-wechat">微信</li>';
        html += '       <li lay-id="tab-alipay">支付宝</li>';
        html += '       <li lay-id="tab-bank">银行卡</li>';
        html += '   </ul>';
        html += '   <div class="layui-tab-content" style="padding-bottom: 0px;">';
        html += '       <div class="layui-tab-item layui-show" style="height: 80px;">';
        if (data != null && data.WechatPath != null) {
            html += '           <div class="paymentcode"><img src="' + data.WechatPath + '" /></div>';
            html += '           <div class="viewBigImage" id="viewWechatImage"><span class="iconfont icon-search"></span>查看大图</div>';
        } else {
            html += '未上传微信收款码！';
        }
        html += '       </div>';
        html += '       <div class="layui-tab-item" style="height: 80px;">';
        if (data != null && data.AlipayPath != null) {
            html += '           <div class="paymentcode"><img src="' + data.AlipayPath + '" /></div>';
            html += '           <div class="viewBigImage" id="viewAlipayImage"><span class="iconfont icon-search"></span>查看大图</div>';
        } else {
            html += '未上传支付宝收款码！';
        }
        html += '       </div>';
        html += '       <div class="layui-tab-item" style="height: 80px;">';
        html += '           <table class="bankInfo">';
        html += '               <tr><td align="right">账户名:</td><td>' + bankAccountName + '</td></tr>';
        html += '               <tr><td align="right">开户行:</td><td>' + bank + '</td></tr>';
        html += '               <tr><td align="right">银行卡号:</td><td>' + bankCardNo + '</td></tr>';
        html += '           </table>';
        html += '       </div>';
        html += '   </div>';
        html += '</div>';

        layer.open({
            type: 1,
            resize: false,
            title: false,
            content: html,
            move: false,
            success: function () {
                $("#viewWechatImage").unbind("click", onViewWechatImageClick);
                $("#viewWechatImage").click(onViewWechatImageClick);
                $("#viewAlipayImage").unbind("click", onViewAlipayImageClick);
                $("#viewAlipayImage").click(onViewAlipayImageClick);
            }
        });
    }

    function onViewWechatImageClick() {
        var self = $(this);
        var src = self.prev().find("img").eq(0).attr("src");
        if (src != "" && src != undefined) {
            layer.open({
                type: 1,
                title: false,
                shadeClose: false,
                resize: false,
                content: '<div class="bigImage"><img src="' + src + '" /></div>'
            });
        }
    }

    function onViewAlipayImageClick() {
        var self = $(this);
        var src = self.prev().find("img").eq(0).attr("src");
        if (src != "" && src != undefined) {
            layer.open({
                type: 1,
                title: false,
                shadeClose: false,
                resize: false,
                content: '<div class="bigImage"><img src="' + src + '" /></div>'
            });
        }
    }

    function onViewVoucherClick() {
        var self = $(this);
        var width = $(window).width();
        var width2 = width * 0.9 - 20;
        var path = self.parent().parent().attr("paymentVoucherImagePath");
        if (path != "" && path != undefined) {
            var paths = path.split(",");

            var html = '';
            html += '<div class="voucherContainer" style="width: ' + width2 + 'px;">';
            html += '   <table>';
            html += '       <tr style="height: 30px;">';
            html += '           <td>缩略图</td>';
            html += '           <td>操作</td>';
            html += '       </tr>';
            for (var i = 0; i < paths.length; i++) {
                html += '   <tr>';
                html += '       <td><img class="smallImg" src="' + paths[i] + '" /></td>';
                html += '       <td><span class="voucher-span-zoom">放大</span></td>';
                html += '   </tr>';
            }
            html += '   </table>';
            html += '</div>';

            layer.open({
                type: 1,
                resize: false,
                title: false,
                content: html,
                move: false,
                success: function () {
                    $(".voucherContainer").find(".voucher-span-zoom").click(onViewVoucherImageClick);
                }
            });
        } else {
            layer.msg(addStyle("未查询到进货凭证！"), { icon: 2, time: 1.5 * 1000, closeBtn: true });
        }
    }

    function onViewVoucherImageClick() {
        var self = $(this);
        var path = self.parent().prev().find("img").attr("src");
        var width = $(window).width();
        var height = $(window).height();
        var width2 = width * 0.9 - 20;
        var height2 = height * 0.9 - 20;
        if (path != "" && path != undefined) {
            layer.open({
                type: 1,
                title: false,
                shadeClose: false,
                resize: false,
                content: '<div class="bigImage" style="width: ' + width2 + 'px; height:' + height2 + 'px;"><img src="' + path + '" /></div>'
            });
        }
    }

    function onViewVoucher2Click() {
        var self = $(this);
        var width = $(window).width();
        var width2 = width * 0.9 - 20;
        var path = self.parent().parent().attr("paymentVoucherImagePath");
        if (path != "" && path != undefined) {
            var paths = path.split(",");

            var html = '';
            html += '<div class="voucherContainer" style="width: ' + width2 + 'px;">';
            html += '   <table>';
            html += '       <tr style="height: 30px;">';
            html += '           <td>缩略图</td>';
            html += '           <td>操作</td>';
            html += '       </tr>';
            for (var i = 0; i < paths.length; i++) {
                html += '   <tr>';
                html += '       <td><img class="smallImg" src="' + paths[i] + '" /></td>';
                html += '       <td><span class="voucher-span-zoom">放大</span></td>';
                html += '   </tr>';
            }
            html += '   </table>';
            html += '</div>';

            layer.open({
                type: 1,
                resize: false,
                title: false,
                content: html,
                move: false,
                success: function () {
                    $(".voucherContainer").find(".voucher-span-zoom").click(onViewVoucherImage2Click);
                }
            });
        } else {
            layer.msg(addStyle("未查询到出货凭证！"), { icon: 2, time: 1.5 * 1000, closeBtn: true });
        }
    }

    function onViewVoucherImage2Click() {
        var self = $(this);
        var path = self.parent().prev().find("img").attr("src");
        var width = $(window).width();
        var height = $(window).height();
        var width2 = width * 0.9 - 20;
        var height2 = height * 0.9 - 20;
        if (path != "" && path != undefined) {
            layer.open({
                type: 1,
                title: false,
                shadeClose: false,
                resize: false,
                content: '<div class="bigImage" style="width: ' + width2 + 'px; height:' + height2 + 'px;"><img src="' + path + '" /></div>'
            });
        }
    }

    function onReceiptClick() {
        var self = $(this);
        var orderId = self.parent().parent().attr("orderId");
        var buyerUserId = self.parent().parent().attr("buyerUserId");

        layer.confirm(addStyle("出货代表你已收到货款，是否确定要出货？"), { icon: 3 }, function () {
            var loadingIndex = layer.msg(addStyle("正在处理 ......"), { icon: 16, shade: 0.01, time: false });
            $.ajax("/Order/Receipt", {
                type: "POST",
                data: {
                    OrderId: orderId,
                    BuyerUserId: buyerUserId
                },
                complete: function (jqXHR, textStatus) {
                    layer.close(loadingIndex);
                },
                success: function (data, textStatus, jqXHR) {
                    //console.log(data);
                    if (data.Success == true) {
                        layer.msg(addStyle("出货成功！"), {
                            icon: 1, time: 1.5 * 1000, end: function () {
                                self.parent().remove();
                            }
                        });
                    } else {
                        layer.msg(addStyle("出货失败: " + data.Msg), { icon: 2, time: 3 * 1000, closeBtn: true });
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    layer.msg(addStyle("系统错误，请联系管理员！"), { icon: 2, time: 3 * 1000, closeBtn: true });
                }
            });
        });
    }

    function onStockGoodClick() {
        var self = $(this);
        var el = self.parent().parent();
        var orderId = el.attr("orderId");
        var sellerUserId = el.attr("sellerUserId");
        var path = el.attr("paymentVoucherImagePath");
        if (path != "" && path != undefined) {
            layer.confirm(addStyle("确定进货吗？"), { icon: 3 }, function () {
                var loadingIndex = layer.msg(addStyle("正在处理 ......"), { icon: 16, shade: 0.01, time: false });
                $.ajax("/Order/SavePaymentImagePath", {
                    type: "POST",
                    data: {
                        OrderId: orderId,
                        Path: path,
                        SellerUserId: sellerUserId
                    },
                    complete: function (jqXHR, textStatus) {
                        layer.close(loadingIndex);
                    },
                    success: function (data, textStatus, jqXHR) {
                        //console.log(data);
                        if (data.Success == true) {
                            layer.msg(addStyle("进货成功！"), {
                                icon: 1, time: 1.5 * 1000, end: function () {
                                    self.parent().parent().find("span[spanBtn='1']").remove();
                                    self.parent().remove();
                                }
                            });
                        } else {
                            layer.msg(addStyle("进货失败: " + data.Msg), { icon: 2, time: 3 * 1000, closeBtn: true });
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        layer.msg(addStyle("系统错误，请联系管理员！"), { icon: 2, time: 3 * 1000, closeBtn: true });
                    }
                });
            });
        } else {
            layer.msg(addStyle("请先上传进货凭证！"), { icon: 2, time: 3 * 1000, closeBtn: true });
        }
    }

    function onPickupClick() {
        var self = $(this);
        var el = self.parent().parent();
        var orderId = el.attr("orderId");
        var sellerUserId = el.attr("sellerUserId");

        layer.confirm(addStyle("提货操作后店铺会给你安排发货，确定要提货吗？"), { icon: 3 }, function () {
            var loadingIndex = layer.msg(addStyle("正在处理 ......"), { icon: 16, shade: 0.01, time: false });
            $.ajax("/Order/Pickup", {
                type: "POST",
                data: {
                    OrderId: orderId,
                    SellerUserId: sellerUserId
                },
                complete: function (jqXHR, textStatus) {
                    layer.close(loadingIndex);
                },
                success: function (data, textStatus, jqXHR) {
                    //console.log(data);
                    if (data.Success == true) {
                        layer.msg(addStyle("提货成功！"), {
                            icon: 1, time: 1.5 * 1000, end: function () {
                                self.parent().remove();
                            }
                        });
                    } else {
                        layer.msg(addStyle("提货失败: " + data.Msg), { icon: 2, time: 3 * 1000, closeBtn: true });
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    layer.msg(addStyle("系统错误，请联系管理员！"), { icon: 2, time: 3 * 1000, closeBtn: true });
                }
            });
        });
    }
});